/**
 * 获取输入时间距今时间
 * @param {String} ('2021-10-5 12:00:00')
 * @returns String
 */
 function dateFormat(time){
    
    let nowTime = new Date()
    let nowTimes = nowTime.getTime()
    let oldTime = new Date(time)
    let oldTimes =oldTime.getTime()
    let diff = (nowTimes - oldTimes )/1000

    if(diff < 10) return '刚刚'
    if(diff < 60) return `${diff}秒前`
    if(diff < 60*60) return `${Math.floor(diff / 60)}分钟前`
    if(diff < 60*60*60) return `${Math.floor(diff / 60/60)}小时前`
    if(diff < 60*60*60*24) return `${Math.floor(diff / 60/60/24)}天前`
    if(diff < 60*60*60*24*30) return `${Math.floor(diff / 60/60/24/30)}月前`
    if(diff < 60*60*60*24*30*12) return `${Math.floor(diff / 60/60/24/30/12)}年前`

}

/**
 * 字符串补0（传递进来的数字长度不够2位就补0
 * @param {String\Number} data
 * @returns  过滤后的数据
 */
function zeroize(data){
    return String(data).length===1 ? '0'+data :data
}



 /**
  * 获取当前时间
  * @param {string} str 
  * @returns String
  */
 
function format(str){
    var d=new Date()
    str=str.replaceAll('Y',d.getFullYear())
    str=str.replaceAll('m',zeroize(d.getMonth()+1))
    str=str.replaceAll('d',zeroize(d.getDate()))
    str=str.replaceAll('H',zeroize(d.getHours()))
    str=str.replaceAll('i',zeroize(d.getMinutes()))
    str=str.replaceAll('s',zeroize(d.getSeconds()))
    return str
}


/**
 * 随机颜色
 * @returns String
 */

 function randomColor(){
    var colorArr=[0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']
    var min=0
    var max=colorArr.length-1
    var one=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var two=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var three=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var four=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var five=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    var six=colorArr[Math.floor(Math.random()*(max-min+1)+min)]
    // console.log('#'+one+two+three+four+five+six);
    return '#'+one+two+three+four+five+six
}

 /**
 * 判断浏览器类型
 * @param {} 
 * @returns {str}
*/
function browser(){

    if(navigator.userAgent.indexOf('Firefox')!== -1){
     return '火狐浏览器'
    } else if(navigator.userAgent.indexOf('Chrome')!== -1){
    return '谷歌浏览器'
    } else if(navigator.userAgent.indexOf('Safari')!== -1){
        return '苹果浏览器'
    } else if(navigator.userAgent.indexOf('Opera')!== -1){
        return 'Opera浏览器'
    } else if(navigator.userAgent.indexOf('Trident')!== -1){
        return 'IE浏览器'
    }
      
} 

/**
 * 判断是PC还是移动端
 * @param {} 
 * @returns {str}
*/
function PcandYd() {
    data = navigator.userAgent
    if ((data.indexOf('Mac')||data.indexOf('Windows')||data.indexOf('Linux'))||data.indexOf('win') !== -1){
        return '电脑端'
    } 
    if ((data.indexOf('iPhone')||data.indexOf('iPad')||data.indexOf('Android')||data.indexOf('Mobile')) !== -1){
        return '移动端'
    } 
}
    


/**
 * 判断是否是微信内置浏览器
 * @param {} 
 * @returns {str}
*/
function weixin(){
    var ua = navigator.userAgent.toLowerCase();
 if(ua.match(/MicroMessenger/i)=="micromessenger") {
         return true;
     } else {
        return false;
     }
 }

 /**
  * 判断标签样式
  * @param object string 举例('divObj',)
  * @return string
  */

 function getStyle(tagObj,cssAttr){
     if(window.getComputedStyle){
         return getComputedStyle(tagObj)[cssAttr] //对象的键taObj是变量
     } else{
         return tagObj.currentStyle[cssAttr]
     }

 }



/**
 * cookie存储
 * @param {string} key 键
 * @param {string} val 数据
 * @param {number} time 时间/s
 */
function setCookie(key, val, time) {
    if (typeof val === 'json') return val = JSON.stringify(val)
    const d = new Date
    d.setTime(d.getTime() - 1000 * 60 * 60 * 8 + 1000 * time)
    document.cookie=`${key}=${val}; expires=${d}`
}


/**
 * 获取cookie
 * @param {string} key 键
 * @returns string
 */
function getCookie(key) {
    let arr = document.cookie.split('; ')
    for (let i = 0; i < arr.length; i++) {
        let temp = arr[i].split('=')
        if (temp[0] === key) {
            return temp[1]
        }
    }

}

/**
 * 删除cookie
 * @param {string} key 
 * return undefined
 */
function removeCookie(key){
    setCookie(key,null,-1)
    
}



/**
 * 获取地址栏参数封装
 * @param {string} key 
 * @returns 
 */
function getParams(key) {
    let query = window.location.search.substring(1)
    query = decodeURI(query)
    let vars = query.split("&")
    for (let i = 0; i < vars.length; i++) {
        let temp = vars[i].split("=")
        if (temp[0] == key) {
            return temp[1]
        }
    }
    return false
}
     
     
